Calculating \hat\theta is simple but obtaining the standard error of \hat\theta requires extra efforts. In this case, we can utilize the convenient linear regression functions in statistical softwares. The sample mean of some function f(W_i) is equivalent to regressing f(W_i) on a vector of 1.
set.seed(1)n <-1000X <-rnorm(n)Y <-1+2* X +rnorm(n)mod <-lm(Y ~ X)gX <- mod$fitted.values# MSE by sample mean mse <-mean((Y - gX)^2)cat("The MSE of gX is", mse)
The MSE of gX is 1.080436
# MSE by linear regressionmse.lm <-lm((Y - gX)^2~1) |>summary()cat("The MSE of gX by linear regression is", mse.lm$coef[1], "with SE", mse.lm$coef[2])
The MSE of gX by linear regression is 1.080436 with SE 0.04810917
We can easily obtain the standard error of the MSE but notice that by doing this, the sum of squared in the MSE is divided by n. In small samples, we need to change the denominator to n - 2 (lost two degrees of freedom in \hat\beta_0 and \hat\beta_1 in gX) by multiplying n/(n - 2) and also adjust the standard error accordingly. After adjustment, the MSE is the same as in the anova table.
mse.lm$coefficients[1] * (n / (n -2))
[1] 1.082601
anova(mod)['Residuals', 'Mean Sq']
[1] 1.082601
Multiple Regressions at One Time
Sometimes, we may want to compare coefficients in two different regressions. In this case, a formal hypothesis test is hard to implement because we need a variance-covariance matrix of the coefficients in different regressions. One easy solution is to trick statistical softwares to run two regression at one time by stacking the two regressions.
Suppose we want to compare the coefficients \alpha and \beta in regressions
Y = X\alpha + \epsilon, \quad \quad Z = D\beta + \nu
We can stack the two samples and obtain a long response variable (Y', Z')' and a large design matrix
\begin{pmatrix}
X & 0 \\
0 & D
\end{pmatrix}
so that we have
\begin{pmatrix} Y \\ Z \end{pmatrix} = \begin{pmatrix} X & 0 \\ 0 & D \end{pmatrix} \begin{pmatrix} \alpha \\ \beta \end{pmatrix} + \begin{pmatrix} \epsilon \\ \nu \end{pmatrix}
By running this stacked regression, we are able to estimate \alpha and \beta at the same time and obtain their variance-covariance matrix. Note clustered standard error is recommended for the stacked regression. This trick is similar to Seemingly Unrelated Regression (SUR), but the benefit is that this method can be extended to IV regressions (or a combination of IV and OLS) by regarding OLS as a special case of IV where the regressors are instrumented by themselves. Below is an example using the dataset of High School and Beyond survey.
hsb2 <- foreign::read.dta("https://stats.idre.ucla.edu/stat/stata/notes/hsb2.dta")hsb2$female <-ifelse(hsb2$female =="female", 1, 0)hsb2$ses <-as.numeric(hsb2$ses)# Separate regressionssummary(lm(read ~ female + ses + socst, data = hsb2))$coef[2, 1:2]
Estimate Std. Error
-1.511128 1.151079
summary(lm(math ~ female + ses + science, data = hsb2))$coef[2, 1:2]